package com.hzcx.app.simplechat.ui.publicui;

import android.Manifest;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.common.Common;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.util.image.ImageUtil;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * 二维码界面
 */
public class QrCodeActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_user_id)
    TextView tvUserId;
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_card_name)
    TextView tvCardName;
    @BindView(R.id.tv_tips)
    TextView tvTips;

    @Override
    public int getLayout() {
        return R.layout.activity_qrcode;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("我的二维码");
        ivTitleRight.setImageResource(R.mipmap.ic_icon_title_more);
        ivTitleRight.setVisibility(View.GONE);

        tvCardName.setText(getResources().getString(R.string.app_name) + "名片");
        tvTips.setText("扫描二维码,加入" + getResources().getString(R.string.app_name));
    }

    @Override
    public void initData() {

//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_icon_login_qq);
//        ivQrcode.setImageBitmap(QRCodeUtil.createQRCodeWithLogo("聊天软件", bitmap));

        GlideUtils.loadImgAsCircle(this, UserInfoUtil.getUserHead(), ivHead);
        tvNickName.setText(UserInfoUtil.getUserNickName());
        tvUserId.setText(getResources().getString(R.string.app_name) + "号：" + UserInfoUtil.getUserNumber());
        GlideUtils.loadImg(this, UserInfoUtil.getUserQrCodeImg(), ivQrcode);
    }

    @OnClick({R.id.tv_save, R.id.iv_title_right, R.id.tv_share})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_save: // 保存图片
                registerPermission();
                break;

            case R.id.iv_title_right: // 更多
                showMoreDialog();
                break;

            case R.id.tv_share: // 分享
                startActivity(new Intent(this, ShareActivity.class)
                        .putExtra(ShareActivity.INTENT_USER_ID, UserInfoUtil.getUserId()));
                break;
        }
    }


    /**
     * 申请文件读写权限
     */
    public void registerPermission() {
        PermissionGen.with(this)
                .addRequestCode(Common.BASIC_PERMISSION_REQUEST_CODE)
                .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE
                        , Manifest.permission.READ_EXTERNAL_STORAGE)
                .request();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    /**
     * 权限申请失败
     */
    @PermissionFail(requestCode = Common.BASIC_PERMISSION_REQUEST_CODE)
    public void openFail() {
        ToastUtils.show("权限获取失败，无法保存图片");
    }

    /**
     * 权限申请成功
     */
    @PermissionSuccess(requestCode = Common.BASIC_PERMISSION_REQUEST_CODE)
    public void openSuccess() {
        ImageUtil.saveMyBitmap(this, ((BitmapDrawable) ivQrcode.getDrawable()).getBitmap(), "chat_" + System.currentTimeMillis() + ".jpg");
    }

    /**
     * 展示更多弹窗
     */
    public void showMoreDialog() {
        List<String> list = new ArrayList<>();
        list.add("换个样式");
        list.add("重置二维码");
        PublicRoundDialog roundDialog = new PublicRoundDialog(this, list, (pos, menu) -> {
            switch (menu) {
                case "换个样式":

                    break;

                case "重置二维码":
                    refreshQrCode();
                    break;
            }
        });
        roundDialog.show();
    }

    /**
     * 重置二维码
     */
    private void refreshQrCode() {
        showConfirmDialog("确定重置二维码", "重置将更新现有二维码名片，以前的二 维码名片都会失效", "重置二维码", new PublicConfirmDialog.OnPublicConfirmClickListener() {
            @Override
            public void publicConfirm() {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
